package systemInformation;

import java.util.ArrayList;
import java.util.List;

import features.SNMPException;
import features.SNMPSession;
import features.SNMPValue;

public class InterfaceInfo {
	private final static String IP_ADDRESS_OID = 	".1.3.6.1.2.1.4.20.1.1";
	
	private final static String IP_AD_ENT_IF_INDEX_OID =".1.3.6.1.2.1.4.20.1.2";
	
	private final static String IP_AD_ENT_NET_MASK_OID =".1.3.6.1.2.1.4.20.1.3";
	
	private final static String IF_INDEX_OID = 		".1.3.6.1.2.1.2.2.1.1";
	
	private final static String DESCRIPTION_OID = 	".1.3.6.1.2.1.2.2.1.2";
	
	private final static String MAC_ADDRESS_OID = 	".1.3.6.1.2.1.2.2.1.6";
	
	private int ifIndex;

	private String ipAddress;

	private String macAddress;
	
	private String description;
	
	private String mask;
	
	public String getMask() {
		return mask;
	}

	public void setMask(String mask) {
		this.mask = mask;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public static List<InterfaceInfo> fromSession(SNMPSession session) throws SNMPException {
		List<InterfaceInfo> interfaces = new ArrayList<InterfaceInfo>();
		
		List<SNMPValue> lstIpAddress = session.getColumn(IP_ADDRESS_OID);
		List<SNMPValue> lstIpAdEntIfIndex = session.getColumn(IP_AD_ENT_IF_INDEX_OID);
		
		List<SNMPValue> lstIfIndex = session.getColumn(IF_INDEX_OID);
		List<SNMPValue> lstMacAddress = session.getColumn(MAC_ADDRESS_OID);
		List<SNMPValue> lstDescription = session.getColumn(DESCRIPTION_OID);
		List<SNMPValue> lstIPMask= session.getColumn(IP_AD_ENT_NET_MASK_OID);
		
		for (int i = 0; i < lstIfIndex.size() && i<lstIpAddress.size(); i++) {
			InterfaceInfo info = new InterfaceInfo();
			
			info.setIpAddress(lstIpAddress.get(i).toString());
			
			int ipAdEntIfIndex = lstIpAdEntIfIndex.get(i).toInt();
			boolean exit = false;
			for (int j = 0; j < lstIfIndex.size() && !exit; j++) {
				if(lstIfIndex.get(j).toInt() == ipAdEntIfIndex) {
					info.setIfIndex(lstIfIndex.get(i).toInt());
					info.setMacAddress(lstMacAddress.get(j).toPhysAddressString());
					info.setDescription(lstDescription.get(j).toString());
					info.setMask((lstIPMask.get(j)).toString());
//					if(info.getMacAddress() != null && !info.getMacAddress().equals("")&& !info.getMacAddress().equals("0:0:0:0:0:0"))
						interfaces.add(info);
					
					exit = true;
				}
			}
		}
		
		return interfaces;
	}
	
	@Override
	public String toString() {
		return "InterfaceInfo [" + ifIndex + ", " + ipAddress + ", " + macAddress + ", " + description +  ", " + mask +"]";
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ifIndex;
		result = prime * result
				+ ((ipAddress == null) ? 0 : ipAddress.hashCode());
		result = prime * result
				+ ((macAddress == null) ? 0 : macAddress.hashCode());
		result = prime * result + ((mask == null) ? 0 : mask.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final InterfaceInfo other = (InterfaceInfo) obj;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (ifIndex != other.ifIndex)
			return false;
		if (ipAddress == null) {
			if (other.ipAddress != null)
				return false;
		} else if (!ipAddress.equals(other.ipAddress))
			return false;
		if (macAddress == null) {
			if (other.macAddress != null)
				return false;
		} else if (!macAddress.equals(other.macAddress))
			return false;
		if (mask == null) {
			if (other.mask != null)
				return false;
		} else if (!mask.equals(other.mask))
			return false;
		return true;
	}

	public int getIfIndex() {
		return this.ifIndex;
	}

	public void setIfIndex(int ifIndex) {
		this.ifIndex = ifIndex;
	}

	public String getIpAddress() {
		return this.ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public String getMacAddress() {
		return this.macAddress;
	}

	public void setMacAddress(String macAddress) {
		this.macAddress = macAddress;
	}
}
